package com.yjd.comm.borrow.convert;

import java.util.ArrayList;
import java.util.List;

import com.yjd.comm.borrow.model.BorrowModel;
import com.yjd.comm.system.enums.DictTypeEnum;
import com.yjd.comm.system.util.DictFieldModel;
import com.yjd.comm.system.util.DictFieldUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.StringUtil;

/**
 * <b>description</b>：借款信息转换器 <br>
 * <b>time</b>：2014-11-15下午4:37:26 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class BorrowModelConvert {
	static List<DictFieldModel> dictFieldModels = new ArrayList<DictFieldModel>();
	static {
		dictFieldModels.add(new DictFieldModel("qclass",
				DictTypeEnum.project_type));
		dictFieldModels.add(new DictFieldModel("limit",
				DictTypeEnum.project_term));
		dictFieldModels.add(new DictFieldModel("mode",
				DictTypeEnum.project_return_type));
		dictFieldModels.add(new DictFieldModel("safe",
				DictTypeEnum.project_danbao));
		dictFieldModels.add(new DictFieldModel("yongtu",
				DictTypeEnum.project_yongtu));
		dictFieldModels.add(new DictFieldModel("status",
				DictTypeEnum.project_status));
		dictFieldModels.add(new DictFieldModel("refuse",
				DictTypeEnum.project_refuse_key));
	}

	public static BorrowModel convert(BorrowModel model) throws Exception {
		if (model != null) {
			DictFieldUtil.disposeModel(model, dictFieldModels);
			model.getStrMap().put("addtime",
					DateUtil.timestampToDateString(model.getAddtime()));
			model.getStrMap().put("starttime",
					DateUtil.timestampToDateString(model.getStarttime()));
			model.getStrMap().put("endtime",
					DateUtil.timestampToDateString(model.getEndtime()));
			model.getStrMap().put(
					"repayday",
					DateUtil.timestampToDateString(model.getRepayday(),
							DateUtil.PATTERN_yyyy_MM_dd));
			if (StringUtil.isNotEmpty(model.getTitle())
					&& model.getTitle().length() > 10) {
				model.getStrMap().put("smallTitle",
						model.getTitle().substring(0, 10) + "...");
			} else {
				model.getStrMap().put("smallTitle", model.getTitle());
			}
		}
		return model;
	}

	public static List<BorrowModel> convert(List<BorrowModel> models)
			throws Exception {
		if (models == null || models.size() == 0) {
			return models;
		}
		for (BorrowModel model : models) {
			convert(model);
		}
		return models;
	}

}
